

#include "iostream"
#include "vector"
#include "unordered_map"
#include "unordered_set"
#include "algorithm"
#include "string"
#include "list"
#include "string.h"

struct State{
    std::vector<int> nums;
    int depth=0;
    State(){

    }
};

class OneLoop {
public:
    std::list<State> queue;
    std::vector<int> final;
    bool reach_final(State &s){
        for(int i=0;i<final.size();i++){
            if(s.nums[i]!=final[i]){
                return false;
            }
        }
        return true;
    }
    bool run(int a
    ) {
        final.resize(a);
        for(int i=0;i<a;i++){
            int cur;std::cin>>cur;
            final[i]=cur;
        }
        State in;
        in.nums.resize(a);
        queue.push_back(in);

        int len=0;
        while (!queue.empty()){
            auto &state=queue.front();
            if(this->reach_final(state)){
                len=state.depth;
                break;
            }
            for(int i=0;i<state.nums.size();i++){
                State s1=state;
                s1.depth++;
                s1.nums[i]++;
                queue.push_back(s1);
            }
            State s2=state;
            s2.depth++;
            for(auto&n:s2.nums){
                n*=2;
            }
            queue.push_back(s2);
        }

        ;

        return true;
    }
};

class Solution {
public:
    void run() {

        bool first = true;
        int a, b, c, d, e;
//        std::cin >> a;

        while (std::cin>>a) {
            if(a==0){
                return;
            }
            OneLoop l;
            if (!l.run(a)) {
//                return;
            }
        }


    }
};

int main() {
//    bool cur_num= true;
    Solution().run();
    return 0;
}

